﻿Imports BUS
Imports DTO
Public Class CapNhatLop

    Public Sub CapNhatLop()
        dtgvdanhsachlop.Rows.Clear()
        Dim _buslop As New ClopBUS()
        Dim _danhsachlop As New List(Of CLopCrt)
        _danhsachlop = _buslop.LayDanhSachLopcrt()
        Dim i As Integer
        For i = 0 To _danhsachlop.Count - 1
            dtgvdanhsachlop.Rows.Add(i + 1, _danhsachlop(i).MaLop, _danhsachlop(i).TenLop, _danhsachlop(i).SiSo, _danhsachlop(i).TenKhoiLop, _danhsachlop(i).SiSoToiDa, _danhsachlop(i).MaKhoiLop)
        Next
    End Sub

    Private Sub CapNhatLop_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim _buskl As New CKhoiLopBUS()    
        cbbkhoilop.DataSource = _buskl.LayDanhSachKhoiLop()
        cbbkhoilop.DisplayMember = "TenKhoiLop"
        cbbkhoilop.ValueMember = "MaKhoiLop"
        CapNhatLop()

    End Sub

    Private Sub btnthoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthoat.Click
        Me.Close()
    End Sub

    Private Sub dtgvdanhsachlop_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtgvdanhsachlop.SelectionChanged

        If dtgvdanhsachlop.CurrentRow.Index >= 0 Then
            txttenlop.Text = dtgvdanhsachlop.CurrentRow.Cells("cltenlop").Value
            cbbkhoilop.Text = dtgvdanhsachlop.CurrentRow.Cells("clkhoilop").Value
            txtsiso.Text = dtgvdanhsachlop.CurrentRow.Cells("clsiso").Value
            txtsisotoida.Text = dtgvdanhsachlop.CurrentRow.Cells("clsisotoida").Value
        End If

    End Sub

    Private Sub btncapnhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncapnhat.Click
        Try
            Dim _lopcrt As New CLopCrt()
            _lopcrt.MaLop = dtgvdanhsachlop.CurrentRow.Cells("clmalop").Value
            _lopcrt.TenLop = txttenlop.Text
            _lopcrt.SiSo = Int32.Parse(txtsiso.Text)
            _lopcrt.SiSoToiDa = Int32.Parse(txtsisotoida.Text)
            Dim _KLdto As New CKhoiLopDTO()
            _KLdto = cbbkhoilop.SelectedItem
            _lopcrt.MaKhoiLop = _KLdto.MaKhoiLop
            Dim _bus As New ClopBUS()

            If dtgvdanhsachlop.CurrentRow.Cells("clmakhoilop").Value <> _lopcrt.MaKhoiLop Then
                If _bus.KiemTraSoLuongLop(_KLdto) > 0 Then
                    If _bus.CapNhatLopCrt(_lopcrt) > 0 Then
                        MessageBox.Show("Cap Nhat Lop Thanh Cong")
                        CapNhatLop()
                    Else
                        MessageBox.Show("Cap Nhat Lop That Bai")
                    End If
                Else
                    MessageBox.Show(" " + _KLdto.TenKhoiLop + " Da Du Lop")
                End If
            Else
                If _bus.CapNhatLopCrt(_lopcrt) > 0 Then
                    MessageBox.Show("Cap Nhat Lop Thanh Cong")
                    CapNhatLop()
                Else
                    MessageBox.Show("Cap Nhat Lop That Bai")
                End If
            End If
            
        Catch ex As Exception
            MessageBox.Show("Loi --> " + ex.ToString())
        End Try

    End Sub
End Class